import { Paster } from "qqlx-core";
import { getPasterKeyTrans } from "qqlx-cdk";

export async function copyText(mess: string) {
  const result = { success: false, message: "当前浏览器，无法支持复制功能，请试试其他浏览器" };
  try {
    const clipboardAPI = navigator.clipboard;
    if (clipboardAPI) {
      await clipboardAPI.writeText(mess);
      result.success = true;
      result.message = `复制 -${mess.slice(0, 10)}...- 成功！`;
    }
  } catch (error) {
    result.message = Object(error).message || "未知错误";
  } finally {
    return result;
  }
}

export function copyPasterText(paster: Paster) {
  const keyTrans = getPasterKeyTrans(paster.type);

  const text = [
    keyTrans.name + `：${paster.name || "-"}\n`,
    keyTrans.desc + `：${paster.desc || "-"}\n`,
    keyTrans.content + `：${paster.content || "-"}\n`,
    keyTrans.remark + `：${paster.remark || "-"}\n`,
  ].join("");

  return copyText(text);
}
